//
//  XZTrainNumController.m
//  生活小助手
//
//  Created by lixiangzhou on 14-8-7.
//  Copyright (c) 2014年 lxz. All rights reserved.
//

#import "XZTrainNumController.h"
#import "XZTrainTool.h"
#import "XZTrainNumParam.h"
#import "XZFunctionKey.h"
#import "MBProgressHUD+MJ.h"
#import "XZTrainNumResultResult.h"
#import "XZTrainNumStation.h"
#import "XZTrainNumResult.h"
#import "XZTrainStationCell.h"
#import "XZTrainNumTrainInfo.h"

@interface XZTrainNumController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic)  UITextField *trainNumField;
@property (assign, nonatomic) BOOL netOK;
@property (strong, nonatomic) NSArray *stationList;
@property (weak, nonatomic) UIView *topView;
@property (strong, nonatomic) UIView *sectionHeaderView;
@property (weak, nonatomic) UITableView *tableView;
@property (strong, nonatomic) UIView *tableHeaderView;
@property (weak, nonatomic) UILabel *trainNumLabel;
@property (weak, nonatomic) UILabel *mileageLabel;
@property (weak, nonatomic) UILabel *fromStationLabel;
@property (weak, nonatomic) UILabel *toLabel;
@property (weak, nonatomic) UILabel *startLabel;
@property (weak, nonatomic) UILabel *arriveLabel;
@end

@implementation XZTrainNumController

#pragma mark - 懒加载
- (UITableView *)tableView
{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] init];
        tableView.sectionHeaderHeight = kTrainSectionHeaderHeight;
        tableView.rowHeight = kTrainRowHeight;
        tableView.width = self.view.width;
        tableView.height = self.view.height - CGRectGetMaxY(self.topView.frame);
        tableView.y = self.view.height - tableView.height;
        tableView.bounces = NO;
        tableView.delegate = self;
        tableView.dataSource = self;
        [self.view addSubview:tableView];
        _tableView = tableView;
    }
    return _tableView;
}

#pragma mark 头标题
- (UIView *)sectionHeaderView
{
    if (!_sectionHeaderView) {
        UIView *headerView = [[UIView alloc] init];
        headerView.height = kTrainSectionHeaderHeight;
        headerView.width = self.view.width;
        headerView.backgroundColor = [UIColor yellowColor];
        
        /* ------------------站点名称------------------*/
        UILabel *stationNameLabel = [[UILabel alloc] init];
        stationNameLabel.font = kTrainSectionFont;
        stationNameLabel.text = @"站点";
        stationNameLabel.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:stationNameLabel];
        
        /* ------------------到达时间------------------*/
        UILabel *arrivedTimeLabel = [[UILabel alloc] init];
        arrivedTimeLabel.font = kTrainSectionFont;
        arrivedTimeLabel.text = @"到达时间";
        arrivedTimeLabel.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:arrivedTimeLabel];
        
        /* ------------------发车时间------------------*/
        UILabel *leaveTimeLabel = [[UILabel alloc] init];
        leaveTimeLabel.font = kTrainSectionFont;
        leaveTimeLabel.text = @"发车时间";
        leaveTimeLabel.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:leaveTimeLabel];
        
        /* ------------------停留------------------*/
        UILabel *stayLabel = [[UILabel alloc] init];
        stayLabel.font = kTrainSectionFont;
        stayLabel.text = @"停留";
        stayLabel.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:stayLabel];
        
        /* ------------------里程------------------*/
        UILabel *mileageLabel = [[UILabel alloc] init];
        mileageLabel.font = kTrainSectionFont;
        mileageLabel.text = @"里程";
        mileageLabel.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:mileageLabel];
        
        /* ------------------设置位置------------------*/
        stationNameLabel.x = 0;
        arrivedTimeLabel.x = kStationNameWidth;
        leaveTimeLabel.x = arrivedTimeLabel.x + kArrivedTimeWidth;
        stayLabel.x = leaveTimeLabel.x + kLeaveTimeWidth;
        mileageLabel.x = stayLabel.x + kStayWidth;
        
        stationNameLabel.width = kStationNameWidth;
        arrivedTimeLabel.width = kArrivedTimeWidth;
        leaveTimeLabel.width = kLeaveTimeWidth;
        stayLabel.width = kStayWidth;
        mileageLabel.width = kMileageWidth;
        
        CGFloat height = headerView.height;
        
        stationNameLabel.height = height;
        arrivedTimeLabel.height = height;
        leaveTimeLabel.height = height;
        stayLabel.height = height;
        mileageLabel.height = height;
        
        _sectionHeaderView = headerView;
    }
    return _sectionHeaderView;
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = kGlobalBackgroundColor;
    self.view.y = 93;
    self.view.height = kScreenHeight - self.view.y;
    
    [self addViews];
}

- (void)addViews
{
    /* ------------------顶部的查找------------------*/
    UIView *topView = [[UIView alloc] init];
    topView.height = 30;
    topView.y = kMargin;
    [self.view addSubview:topView];
    self.topView = topView;
    
    UILabel *label = [[UILabel alloc] init];
    label.font = kTrainBigFont;
    label.text = @"车次";
    CGSize size = [label.text sizeWithAttributes:@{NSFontAttributeName: label.font}];
    label.width = size.width;
    label.height = topView.height;
    [topView addSubview:label];
    
    NSString *placeholder = @"请输入车次，如T10";
    UITextField *trainNumField = [UITextField textFieldWithPlaceholder:placeholder leftMargin:5];
    trainNumField.font = kTrainNormalFont;
    CGSize numSize = [placeholder sizeWithAttributes:@{NSFontAttributeName: trainNumField.font}];
    trainNumField.x = CGRectGetMaxX(label.frame) + kMargin;
    trainNumField.width = numSize.width + kMargin;
    trainNumField.height = label.height;
    self.trainNumField = trainNumField;
    [topView addSubview:trainNumField];
    
    UIButton *queryBtn = [[UIButton alloc] init];
    [queryBtn setTitle:@"查询" forState:UIControlStateNormal];
    [queryBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    queryBtn.backgroundColor = [UIColor orangeColor];
    queryBtn.x = CGRectGetMaxX(trainNumField.frame) + kMargin;
    queryBtn.width = 60;
    queryBtn.height = topView.height;
    [queryBtn addTarget:self action:@selector(getTrainInfo) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:queryBtn];

    topView.width = CGRectGetMaxX(queryBtn.frame) - CGRectGetMinX(label.frame);
    topView.centerX = self.view.centerX;
    
}

#pragma mark 按钮点击处理
- (void)getTrainInfo
{
    NSString *trainNum = self.trainNumField.text;
    [self.view endEditing:YES];
    
    [self sendRequest:trainNum];
}

#pragma mark 发送请求
- (void)sendRequest:(NSString *)trainNum
{
    XZTrainNumParam *param = [[XZTrainNumParam alloc] init];
    param.key = [XZFunctionKey trainKey];
    param.name = trainNum;
    param.dtype = @"json";
    
    [MBProgressHUD showMessage:@"正在帮您查询"];
    
    [XZTrainTool trainNumWithParameter:param success:^(XZTrainNumResult *responseObj) {
        self.netOK = YES;
        [self processResult:responseObj];
        [MBProgressHUD hideHUD];
    } failure:^(NSError *error) {
        self.netOK = YES;
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:error.domain];
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (!self.netOK) {
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"请检查您的网络"];
        }
        
    });
}

#pragma mark 处理请求结果
- (void)processResult:(XZTrainNumResult *)responseObj
{
    XZTrainNumResultResult *result = responseObj.result;
    XZTrainNumTrainInfo *trainInfo = result.train_info;
    self.stationList = result.station_list;
    
    [self setTableViewHeader:trainInfo];
    
    [self.tableView reloadData];
}

- (void)setTrainNo:(NSString *)trainNo
{
    _trainNo = trainNo;
    
    self.trainNumField.text = trainNo;
    
    [self sendRequest:trainNo];
}

#pragma mark - 设置表头
- (UIView *)setTableViewHeader:(XZTrainNumTrainInfo *)trainInfo
{
    if (!_tableHeaderView) {
        UIView *headerView = [[UIView alloc] init];
        
        CGFloat LeftX = 20;
        CGFloat width = self.view.width * 0.5 - LeftX;
        CGFloat rowHeight = 21;
        CGFloat rightX = LeftX + width;
        /* ------------------列次名称------------------*/
        UILabel *trainNumLabel = [[UILabel alloc] init];
        trainNumLabel.width = width;
        trainNumLabel.height = rowHeight;
        trainNumLabel.x = LeftX;
        trainNumLabel.font = kTrainHeaderFont;
        [headerView addSubview:trainNumLabel];
        self.trainNumLabel = trainNumLabel;
        
        /* ------------------里程------------------*/
        UILabel *mileageLabel = [[UILabel alloc] init];
        mileageLabel.x = rightX;
        mileageLabel.width = width;
        mileageLabel.height = rowHeight;
        mileageLabel.font = kTrainHeaderFont;
        [headerView addSubview:mileageLabel];
        self.mileageLabel = mileageLabel;
        
        /* ------------------起点站------------------*/
        UILabel *fromStationLabel = [[UILabel alloc] init];
        fromStationLabel.width = width;
        fromStationLabel.height = rowHeight;
        fromStationLabel.y = CGRectGetMaxY(trainNumLabel.frame);
        fromStationLabel.x = LeftX;
        fromStationLabel.font = kTrainHeaderFont;
        [headerView addSubview:fromStationLabel];
        self.fromStationLabel = fromStationLabel;
        
        /* ------------------终点站------------------*/
        UILabel *toLabel = [[UILabel alloc] init];
        toLabel.width = width;
        toLabel.height = rowHeight;
        toLabel.x = rightX;
        toLabel.y = CGRectGetMaxY(trainNumLabel.frame);
        toLabel.font = kTrainHeaderFont;
        [headerView addSubview:toLabel];
        self.fromStationLabel = fromStationLabel;
        
        /* ------------------发车时间------------------*/
        UILabel *startLabel = [[UILabel alloc] init];
        startLabel.width = width;
        startLabel.height = rowHeight;
        startLabel.y = CGRectGetMaxY(fromStationLabel.frame);
        startLabel.x = LeftX;
        startLabel.font = kTrainHeaderFont;
        [headerView addSubview:startLabel];
        self.startLabel = startLabel;
        
        /* ------------------到达时间------------------*/
        UILabel *arriveLabel = [[UILabel alloc] init];
        arriveLabel.width = width;
        arriveLabel.height = rowHeight;
        arriveLabel.x = rightX;
        arriveLabel.y = CGRectGetMaxY(fromStationLabel.frame);
        arriveLabel.font = kTrainHeaderFont;
        [headerView addSubview:arriveLabel];
        self.arriveLabel = arriveLabel;
        
        headerView.height = CGRectGetMaxY(arriveLabel.frame);
        
        _tableHeaderView = headerView;
    }
    self.trainNumLabel.text = [NSString stringWithFormat:@"列次名称：%@", trainInfo.name];
    self.mileageLabel.text =[NSString stringWithFormat:@"里程：%@", trainInfo.mileage];
    self.fromStationLabel.text = [NSString stringWithFormat:@"起点站：%@", trainInfo.start];
    self.toLabel.text = [NSString stringWithFormat:@"终点站：%@", trainInfo.end];
    self.startLabel.text = [NSString stringWithFormat:@"发车时间：%@", trainInfo.starttime];
    self.arriveLabel.text = [NSString stringWithFormat:@"到达时间：%@", trainInfo.endtime];
    
    self.tableView.tableHeaderView = _tableHeaderView;
    return _tableHeaderView;

}

#pragma mark 退出键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

#pragma mark - UITableViewDataSource, UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XZTrainStationCell *cell = [XZTrainStationCell cellWithTableView:tableView];
    cell.trainStation = self.stationList[indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.stationList.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return self.sectionHeaderView;
}



@end
